package com.some;

import java.util.Arrays;
import java.util.Scanner;

public class ByteDance5_6_1 {
    public int maxVal(int m, int[] time, int[] val) {
        int max = 0;
        for (int i = 0; i < time.length && time[i] <= m; i++) {
            for (int j = i + 1; j < time.length && (time[i] + time[j] <= m); j++) {
                max = Math.max(max, (val[i] + val[j]));
            }
        }
        return max;
    }

    public int maxVal_2(int maxW, int[] time, int[] val) {
        int max = 0;
        int[] volume = new int[time.length];
        int maxV = 2;
        Arrays.fill(volume, 1);
        int[][] dp = new int[maxV + 1][maxW + 1];
        for (int i = 0; i < volume.length; i++) {
            int count = 0;
            for (int j = maxV; j >= volume[i]; j--) {
                for (int k = maxW; k >= time[i]; k--) {
                    dp[j][k] = Math.max(dp[j][k], dp[j - volume[i]][k - time[i]] + val[i]);
                    if (dp[j][k] <= dp[j - volume[i]][k - time[i]] + val[i]) {
                        count++;
                        if (count == 2) {
                            max = Math.max(max, dp[j][k]);
                        }
                    }

                }
            }
        }
        return max;
    }
}

class Main_5_6_1 {
    public static void main(String[] args) {
        ByteDance5_6_1 byteDance5_6_1 = new ByteDance5_6_1();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] time = new int[n];
        int[] val = new int[m];
        for (int i = 0; i < n; i++) {
            time[i] = sc.nextInt();
            val[i] = sc.nextInt();
        }
        System.out.println(byteDance5_6_1.maxVal_2(m, time, val));
    }
}
